aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/items/[id]/+page.server.ts
blob: 67cebd93488a28cb6a3c0760cf9868426f5c1e25 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { fd, auth, get, post } from "$lib/utils";
import { fail, redirect } from "@sveltejs/kit";

export let load = async ({ params: { id } }) => {
  let item = await get(`/items/${id}`);
  return { item };
};

export const actions = {
  default: async ({ cookies, request, url }) => {
    let form = await fd(request);
    try {
      await post(`/items`, form, auth(cookies));
    } catch (e: any) {
      return fail(400, { message: e.message });
    }

    let success = `/${url.pathname.split("/")[1]}`;
    redirect(307, success);
  },
};